<?php

namespace Sharks\Core;

use Sharks\Core\Cfg\Configuration;
use Sharks\Core\Path\ModulePathKit;
use Sharks\Core\Support\Bag\ParameterBag;

/**
 * 模块管理集
 */
class Module {

    private static $collection = null;

    /**
     * 加入模块
     * @param Module $module
     */
    public static function add(Module $module) {
        if (is_null(self::$collection)) {
            self::$collection = new ParameterBag;
        }
        if (self::$collection->has($module->name)) {
            return;
        }
        self::$collection->put($module->name, $module);
    }

    /**
     * 按模块名获取
     * @param $name
     * @return null
     */
    public static function get($name) {
        if (is_null(self::$collection)) {
            self::$collection = new ParameterBag;
            return null;
        }
        return self::$collection->get($name);
    }

    /**
     * 所有模块
     * @return array
     */
    public static function all() {
        if (is_null(self::$collection)) {
            self::$collection = new ParameterBag;
            return array();
        }
        return self::$collection->all();
    }

    /** 名称 */
    protected $name;

    /** 模块本机物理位置 */
    protected $path;

    /** 配置 */
    protected $cfg;

    /** 加载器 */
    protected $loader;

    public function __construct($name, $modulePath, $loader) {
        $this->name = $name;
        $this->path = new ModulePathKit($modulePath);
        $this->cfg = new Configuration($this->path->resource . "/config.yml");
        $this->loader = $loader;
    }

    public function name() {
        return $this->name;
    }

    public function path() {
        return $this->path;
    }

    public function cfg() {
        return $this->cfg;
    }

    public function loader() {
        return $this->loader;
    }

}